﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace BuXiaChu.OrderDishes.Model
{
    public class PageBarModel
    {
        #region Private Reg
        private Regex indexReg = new Regex("{index}", RegexOptions.IgnoreCase);
        private Regex sizeReg = new Regex("{size}", RegexOptions.IgnoreCase);
        private Regex countReg = new Regex("{count}", RegexOptions.IgnoreCase);
        private Regex onclickCallbackReg = new Regex("{ajaxOnClickCallback}", RegexOptions.IgnoreCase);
        //href 标识
        private Regex hrefReg = new Regex("{href}", RegexOptions.IgnoreCase);
        //text 标识
        private Regex textReg = new Regex("{text}", RegexOptions.IgnoreCase);
        #endregion

        public PageBarModel()
        {
            ChangePageUrlParamFormat = "pageIndex={0}&pageSize={1}";
            IsForAjax = true;
            PageButtonCount = 10;
            ChangePageUrl = "";
            AjaxPageClickCallback = "PageBar_OnChange({index},{size},{count})";
        }

        #region
        public int CurrentIndex
        {
            get;
            set;
        }

        public int Size
        {
            get;
            set;
        }

        public int Count
        {
            get;
            set;
        }

        /// <summary>
        /// 分页按钮数 Default:10
        /// </summary>
        public int PageButtonCount
        {
            get;
            set;
        }

        /// <summary>
        /// 显示跳转文本框
        /// </summary>
        public bool ShowGotoInputText
        {
            get;
            set;
        }

        public bool ShowPageInfo
        {
            get;
            set;
        }

        public bool IsForAjax
        {
            get;
            set;
        }

        /// <summary>
        /// default PageBar_OnChange({index},{size},{count})
        /// </summary>
        public string AjaxPageClickCallback
        {
            get;
            set;
        }

        public string ChangePageUrl
        {
            get;
            set;
        }

        /// <summary>
        /// default  pageIndex={0}&amp;pageSize={1} 
        /// </summary>
        public string ChangePageUrlParamFormat
        {
            get;
            set;
        }

        public string FormatChangePageUrl(int pageIndex)
        {
            #region
            var param = string.Format(ChangePageUrlParamFormat, pageIndex, Size);
            if (this.ChangePageUrl.IndexOf("?") > 0)
            {
                return this.ChangePageUrl + "&" + param;
            }

            return this.ChangePageUrl + "?" + param;
            #endregion
        }

        public int StartPageIndex
        {
            #region
            get
            {
                int midpage = (int)((CurrentIndex - 1) / PageButtonCount);
                int pageoffset = midpage * PageButtonCount;
                int endpage = ((pageoffset + PageButtonCount) > Count) ? Count : (pageoffset + PageButtonCount);

                return pageoffset + 1;
            }
            #endregion
        }

        public int EndPageIndex
        {
            #region
            get
            {
                int midpage = (int)((CurrentIndex - 1) / PageButtonCount);
                int pageoffset = midpage * PageButtonCount;
                int endpage = ((pageoffset + PageButtonCount) > Count) ? Count : (pageoffset + PageButtonCount);

                return endpage;
            }
            #endregion
        }
        #endregion

        #region Render Html
        public string RenderContentPageHtml(string htmlText, string currentHtmlText)
        {
            #region
            int i = this.StartPageIndex, endPage = this.EndPageIndex;
            var htmlContent = "";
            for (; i <= endPage; i++)
            {
                if (i != CurrentIndex)
                {
                    htmlContent += ReplacePageBarHtmlTag(htmlText, i);
                }
                else
                {
                    htmlContent += ReplacePageBarHtmlTag(currentHtmlText, i);
                }

            }
            return htmlContent;
            #endregion
        }

        public string RenderPreviousPageHtml(string htmlText)
        {
            int preIndex = CurrentIndex > 1 ? CurrentIndex - 1 : -1;
            return ReplacePageBarHtmlTag(htmlText, preIndex);
        }

        public string RenderNextPageHtml(string htmlText)
        {
            int nexIndex = CurrentIndex < this.Count ? CurrentIndex + 1 : -1;
            return ReplacePageBarHtmlTag(htmlText, nexIndex);
        }

        public string RenderFirstPageHtml(string htmlText)
        {
            int firstIndex = CurrentIndex != 1 ? 1 : -1;
            return ReplacePageBarHtmlTag(htmlText, firstIndex);
        }

        public string RenderLastPageHtml(string htmlText)
        {
            int lastIndex = CurrentIndex != this.Count ? Count : -1;
            return ReplacePageBarHtmlTag(htmlText, lastIndex);
        }

        public string RenderPageInfoHtml(string text)
        {
            return ShowPageInfo ? string.Format(text, CurrentIndex, Count) : "";

        }
        #endregion

        #region Private Method

        private string ReplacePageBarHtmlTag(string htmlText, int index)
        {
            #region
            //替换ajaxOnClickCallback标识
            htmlText = ReplaceAjaxPageClickCallbackTag(htmlText, index);

            //href 
            if (index > 0)
            {
                htmlText = hrefReg.Replace(htmlText, FormatChangePageUrl(index));
            }
            else
            {
                htmlText = hrefReg.Replace(htmlText, "#");
            }

            htmlText = textReg.Replace(htmlText, index.ToString());

            return htmlText;
            #endregion
        }

        /// <summary>
        /// Index 小于 0 将不调用Ajax回调函数
        /// </summary>
        /// <returns></returns>
        private string ReplaceAjaxPageClickCallbackTag(string htmlText, int index)
        {
            #region

            if (!string.IsNullOrEmpty(AjaxPageClickCallback) && IsForAjax && index >= 0)
            {
                var text = indexReg.Replace(AjaxPageClickCallback, index.ToString());
                text = sizeReg.Replace(text, Size.ToString());
                text = countReg.Replace(text, Count.ToString());
                text = "onclick=\"" + text.Replace("\"", "'") + " ;return false;\"";

                return onclickCallbackReg.Replace(htmlText, text);
            }
            else
            {
                return onclickCallbackReg.Replace(htmlText, "");
            }

            #endregion
        }

        #endregion
    }
}
